require('./api')
    //let _pick = require('lodash/pick')
    //let _reduce = require('lodash/reduce')

//https://www.npmjs.com/package/validatorjs
import Validator from 'validatorjs'
Validator.useLang('zh');
Validator.register('mobile', function(value, requirement, attribute) {
    return (value + '').match(/^1[0-9]{10}$/);
}, ':attribute应为11位数字.');

Validator.register('digits2', function(value, requirement, attribute) {
    return (value + '').match(/^\d+(\.\d{1,2})?$/);
}, ':attribute应为数字，并允许有两位小数.');

/***
  获取id下的具体class=inV的数据
*/
function toJson(id) {
    return json($api.byId(id), '.inV')
}

function json(el, selector) {
    var inputDOM = $api.domAll(el, selector)
    var o = {};
    for (var i = 0, len = inputDOM.length; i < len; i++) {
        let name = inputDOM[i].name || inputDOM[i].id
        let e = inputDOM[i]
        if (e.type === 'checkbox') {
            if (e.checked&&!e.disabled) {
                if (o[name]) {
                    o[name].push(e.value || '')
                } else {
                    o[name] = [e.value]
                }
            }
        } else if (e.type === 'radio') {
            if (e.checked) {
                o[name] = e.value || ''
            }
        } else {
            o[name] = $api.val(e) || ''
        }
    }
    return o
}

let checkAll = (el, selector, val) => {
    var inputDOM = $api.domAll(el, selector)
    for (var i = 0, len = inputDOM.length; i < len; i++) {
        let e = inputDOM[i]
        if (e.type === 'checkbox'&&!e.disabled) {
            e.checked = val
        }
    }
}

let checkEnable = (el, selector, bval) => {
    var inputDOM = $api.domAll(el, selector)
    for (var i = 0, len = inputDOM.length; i < len; i++) {
        let e = inputDOM[i]
        if (e.type === 'checkbox') {
            if (bval) {
                e.removeAttribute('disabled');
            } else {
                e.setAttribute('disabled', 'disabled')
            }
        }
    }
}

let pick = (validFields, key1, key2) => {
    var ret = {}
    validFields.forEach(function(it) {
        ret[it[key1]] = it[key2]
    })
    return ret
}

class Valid {
    constructor(valFileds) {
        this.valFileds = valFileds;
        this.rules = pick(valFileds, 'attribute', 'rule')
        this.attributeNames = pick(valFileds, 'attribute', 'name')
    }
    doValidate(data) {
        this.valid = new Validator(data, this.rules)
        this.valid.setAttributeNames(this.attributeNames)
        return this
    }
    passes() {
        return this.valid.passes()
    }
    hasErr() {
        return this.valid.errors.errorCount() !== 0
    }
    firstErr() {
        let ret = ''
        for (var i = 0; i < this.valFileds.length; i++) {
            var attr = this.valFileds[i].attribute;
            if (this.valid.errors.has(attr)) {
                ret = this.valid.errors.first(attr)
                break
            }
        }
        return ret
    }
}

export default {
    toJson,
    json,
    checkAll,
    checkEnable,
    Validator,
    Valid
}
